package tictactoe.protocol;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import tictactoe.Client;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Double
 * Date: 16.01.12
 * Time: 3:05
 * To change this template use File | Settings | File Templates.
 */
public class ProtocolTest extends TestCase
{
	ArrayList<Client> clients = new ArrayList<Client>();
	ArrayList<Socket> sockets = new ArrayList<Socket>();
	Client c;
	ServerSocket ss;

	public ProtocolTest() throws IOException
	{

		/*for(int i=0;i<3;i++)
		{
			sockets.add(new Socket("127.0.0.1",65222));
			clients.add(new Client(ss.accept()));

		}       */
		/*new Socket("127.0.0.1",2020);
		c = new Client(ss.accept());

		new Socket("127.0.0.1",2020);
		clients.add(new Client(ss.accept())); */
		c = new Client();
		clients.add(new Client());
		clients.get(0).setName("Misha");
		c.setName("Vasya");
	}
	@Before
	public void setUp() throws Exception
	{
		ss = new ServerSocket(2070);
	}
	@After
	public void tearDown() throws Exception
	{
		ss.close();
	}

	@Test (expected=IndexOutOfBoundsException.class) public void elementAt() {
		int[] intArray = new int[10];

		int i = intArray[20]; // Should throw IndexOutOfBoundsException
	}
	@Test
	public void testDecodeSystemPacketAuth() throws Exception
	{
		//System.out.println(new Gson().toJson(new Packet(Protocol.status,Protocol.authPassed)));
		
		Assert.assertTrue(Protocol.decodeSystemPacket("{\"Type\":\"Auth\",\"Data\":\"Auth\"}",c,clients));

	}

	@Test(expected=java.lang.NullPointerException.class) public void testDecodeSystemPacketStatus()
	{
		//System.out.println(new Gson().toJson(new Packet(Protocol.status,Protocol.authPassed)));

		Assert.assertTrue(!Protocol.decodeSystemPacket("{\"Type\":\"Status\",\"Data\":\"Status\"}",c,clients));

	}
	@Test (expected=java.lang.NullPointerException.class) public void testDecodeSystemPacketSetPartner()
	{
		//System.out.println(new Gson().toJson(new Packet(Protocol.status,Protocol.authPassed)));
		Protocol.decodeSystemPacket("{\"Type\":\"SetPartner\",\"Data\":\"Misha\"}",c,clients);
		//Assert.assertTrue(Protocol.decodeSystemPacket("{\"Type\":\"SetPartner\",\"Data\":\"Misha\"}",c,clients));


	}
	@Test (expected=java.lang.NullPointerException.class) public void testDecodeSystemPacketGetUsers()
	{
		//System.out.println(new Gson().toJson(new Packet(Protocol.status,Protocol.authPassed)));

		Assert.assertTrue(Protocol.decodeSystemPacket("{\"Type\":\"GetUsers\",\"Data\":\"null\"}",c,clients));

	}

	@Test
	public void testDecodeGamePacket() throws Exception
	{

	}

	@Test
	public void testEncode() throws Exception
	{


	}
}
